# -*- coding:utf-8 -*-
"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: ${DATE}:${TIME}
@product: ${PRODUCT_NAME}
=============================
"""

import sys
from PyQt5.QtWidgets import QDialog, QApplication

import gtyTools
from uiFiles import ui_machineSetting

import operate
import timeDateSetting
import softwareUpdate
from gtyConfig import language, configFileHandler,systemConfig
import networkSetting
import advancedSetting
import uiTools
import logDialog
import activeSetting
import settings


class ConfigMachineSelectDialog(QDialog, ui_machineSetting.Ui_machineSetting):

    def __init__(self, mainPage, eventQ, parent=None):
        super(ConfigMachineSelectDialog, self).__init__(parent)
        self.setupUi(self)
        uiTools.centerAndSetIcon(self)
        self.mainPage = mainPage
        self.eventQ = eventQ

        # 建立链接
        self.operateBtn.clicked.connect(self.operate)
        self.activeSettingBtn.clicked.connect(self.activeSetting)
        self.timeDateSettingBtn.clicked.connect(self.timeAndDateSetting)
        self.updateBtn.clicked.connect(self.softwareUpdateDialog)
        self.networkBtn.clicked.connect(self.networkSetting)
        self.advancedSetBtn.clicked.connect(self.advancedSetting)
        self.logBtn.clicked.connect(self.logSetting)
        self.settingsBtn.clicked.connect(self.settings)

        # 处理不同的语言
        self.operateBtn.setText(language.configMachine_passiveSystem)
        self.activeSettingBtn.setText(language.configMachine_activeSystem)
        self.timeDateSettingBtn.setText(language.configMachine_timeDateSetting)
        self.updateBtn.setText(language.configMachine_softwareUpdate)
        self.networkBtn.setText(language.configMachine_networkSetting)
        self.returnBtn.setText(language.configMachine_back)
        self.settingsBtn.setText(language.configMachine_settings)
        self.advancedSetBtn.setText(language.configMachine_advancedSetting)
        self.logBtn.setText(language.logDialog_name)
        self.setWindowTitle("Machine Config")
        self.setGeometry(-200, -200, self.width(), self.height())

        # 隐藏部分接口
        self.machine = configFileHandler.UserConfigHandler()
        self.hardwareType = self.machine.read('machine','hardwareType','string','F2000').upper()
        if self.hardwareType == 'A400':
            self.operateBtn.hide()
        if self.hardwareType == 'F800' or self.hardwareType == 'T400':
            self.activeSettingBtn.hide()

        if self.hardwareType == 'F2000':
            #启动界面读配置文件来设置有源无源是否可点
            self.setPassOrActBtn()
            #根据设置来设置有源无源是否可点
            self.mainPage.eventEngine.eventList["ui_setPassOrAct"].connect(self.btnEnable)

    # 发生事件
    def sendEvent(self, task, eventName, eventData=None):
        if eventData is None:
            eventData = []
        e = [eventName, eventData]
        try:
            if task.upper() in self.eventQ.keys():
                self.eventQ[task.upper()].put(e)
        except Exception as e:
            print(e)

    # 无源系统
    def operate(self):
        e = operate.Operate(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    # 有源系统
    def activeSetting(self):
        e = activeSetting.ActiveSetting(self.mainPage, self.eventQ)
        if e.exec_():
            pass
        pass

    # 时间日期设置
    def timeAndDateSetting(self):
        e = timeDateSetting.TimeDateSetting(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    # 软件升级
    def softwareUpdateDialog(self):
        e = softwareUpdate.SoftwareUpdate(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    # 网络设置
    def networkSetting(self):
        e = networkSetting.NetworkSetting(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    # 系统设置
    def settings(self):
        e = settings.Settings(self.mainPage,self.eventQ)
        if e.exec_():
            pass

    # 高级设置，直接修改配置文件
    def advancedSetting(self):
        e = advancedSetting.AdvancedSetting(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    # 日志
    def logSetting(self):
        e = logDialog.LogDialog(self.mainPage, self.eventQ)
        if e.exec_():
            pass

    #读取配置文件设置有源无源按钮是否可点
    def setPassOrActBtn(self):
        if self.machine.read('machine', 'machineState') == 'Active':
            self.operateBtn.setDisabled(True)
            self.activeSettingBtn.setDisabled(False)
            self.operateBtn.setStyleSheet(systemConfig.param.btnDisabledStyle)
            self.activeSettingBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)
        else:
            self.activeSettingBtn.setDisabled(True)
            self.operateBtn.setDisabled(False)
            self.activeSettingBtn.setStyleSheet(systemConfig.param.btnDisabledStyle)
            self.operateBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)

    #设置有源无源时接收信号设置按钮是否可点
    def btnEnable(self,str):
        if str == 'Passive':
            self.activeSettingBtn.setDisabled(True)
            self.operateBtn.setDisabled(False)
            self.activeSettingBtn.setStyleSheet(systemConfig.param.btnDisabledStyle)
            self.operateBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)
        else:
            self.operateBtn.setDisabled(True)
            self.activeSettingBtn.setDisabled(False)
            self.operateBtn.setStyleSheet(systemConfig.param.btnDisabledStyle)
            self.activeSettingBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = ConfigMachineSelectDialog(None, None, None, None)
    form.show()
    sys.exit(app.exec_())
